// ClientContextAwareValueConverter.cs
//
// Copyright (c) 2009 Mike Strobel
//
// This source code is subject to the terms of the Microsoft Reciprocal License (Ms-RL).
// For details, see <http://www.opensource.org/licenses/ms-rl.html>.
//
// All other rights reserved.

using System;
using System.Globalization;
using System.Threading;
using System.Windows.Data;

using Microsoft.Practices.ServiceLocation;

using Supremacy.Resources;

namespace Supremacy.Client
{
    public abstract class ClientContextAwareValueConverter : IValueConverter
    {
        private static IResourceManager s_resourceManager;
        private static IClientContext s_clientContext;

        // ReSharper disable MemberCanBeMadeStatic.Global
        protected IResourceManager ResourceManager
        {
            get
            {
                var resourceManager = s_resourceManager;
                if (resourceManager == null)
                {
                    var newResourceManager = ServiceLocator.Current.GetInstance<IResourceManager>();
                    resourceManager = Interlocked.CompareExchange(ref s_resourceManager, resourceManager, null) ?? newResourceManager;
                }
                return resourceManager;
            }
        }

        protected IClientContext ClientContext
        {
            get
            {
                var clientContext = s_clientContext;
                if (clientContext == null)
                {
                    var newClientContext = ServiceLocator.Current.GetInstance<IClientContext>();
                    clientContext = Interlocked.CompareExchange(ref s_clientContext, clientContext, null) ?? newClientContext;
                }
                return clientContext;
            }
        }
        // ReSharper restore MemberCanBeMadeStatic.Global

        #region Implementation of IValueConverter
        public abstract object Convert(object value, Type targetType, object parameter, CultureInfo culture);
        
        public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
        #endregion
    }
}